package com.hyt.it.ogt.kq.main.controller.gov;


import com.alibaba.fastjson.JSON;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskKsConfig;
import com.hyt.it.ogt.kq.service.gov.model.vo.tempinterface.UnifiedAdmissionNumberParamVO;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeTaskExamSysConfigService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskKsConfigService;
import com.hyt.it.ogt.kq.service.gov.service.ITimeRoomCandidateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 考生编排信息 前端控制器
 * </p>
 *
 * @author chengxh
 * @since 2022-02-15
 */
@Api(tags = "199.临时接口", value = "政企考务临时接口开发统一放在这个controller")
@ApiSort(value = 199)
@RestController
@RequestMapping("/kw/gov/tempInterface")
@Slf4j
public class TempInterfaceController extends BaseController {

    @Resource
    private ITimeRoomCandidateService iTimeRoomCandidateService;
    @Resource
    private IOfficeTaskExamSysConfigService iOfficeTaskExamSysConfigService;
    @Resource
    private ITaskKsConfigService iTaskKsConfigService;

    @ApiOperation(
            value = "99.1 （深港澳）统一准考证号",
            notes = "深港澳项目需求，模拟测试、仿真测试、正式考试统一使用一个准考证号。"
    )
    @ApiOperationSort(value = 1)
    @PostMapping("/unifiedAdmissionNumber")
    public ApiResponse<Object> unifiedAdmissionNumber(@RequestBody UnifiedAdmissionNumberParamVO unifiedAdmissionNumberParamVO) {
        log.info("统一准考证号参数: {} ", JSON.toJSONString(unifiedAdmissionNumberParamVO));
        try {
            Integer count = iTimeRoomCandidateService.unifiedAdmissionNumber(unifiedAdmissionNumberParamVO);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data("还有"+ count +"条编排数据未分配准考证号，请手动处理！")
                    .build();
        } catch (KqException k) {
            log.error("# 统一准考证号异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("统一准考证号失败。", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_UNIFIED_ADMISSION_NUMBER.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "99.2 （深港澳）v2.3.4.p4数据库脚本执行。",
            notes = "执行数据库脚本"
    )
    @ApiOperationSort(value = 2)
    @PostMapping("/updateSQL_2_3_4_p4")
    public ApiResponse<Object> updateSQL_2_3_4_p4() {

        try {

            iOfficeTaskExamSysConfigService.updateSQL2_3_4_p4();

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# （深港澳）v2.3.4.p4数据库脚本执行异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("（深港澳）v2.3.4.p4数据库脚本执行失败。", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_UNIFIED_ADMISSION_NUMBER.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "99.3 （深港澳）更新翻译参数值",
            notes = "更新翻译参数值，如果当前是关闭状态，则打开，如果是打开状态，则关闭。"
    )
    @ApiOperationSort(value = 3)
    @PostMapping("/updateTranslateConfigCode")
    public ApiResponse<Object> updateTranslateOpen(String taskId) {
        log.info("（深港澳）更新翻译参数值的参数：{}",taskId);
        try {

            TaskKsConfig taskKsConfig = iTaskKsConfigService.updateTranslateOpen(taskId);

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(taskKsConfig)
                    .build();
        } catch (KqException k) {
            log.error("# 深港澳）更新翻译参数值执行异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("深港澳）更新翻译参数值执行失败。", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_UNIFIED_TRANSLATE.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "99.4 updateSQL2_3_4_p12",
            notes = "添加线上任务参数,如果没有则添加，否则不添加。"
    )
    @ApiOperationSort(value = 4)
    @PostMapping("/updateSkipConfigCode")
    public ApiResponse<Object> updateSkipConfigCode() {
        try {

            iOfficeTaskExamSysConfigService.updateSQL2_3_4_p12();

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# updateSQL2_3_4_p12添加线上任务参数异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("updateSQL2_3_4_p12添加线上任务参数失败。", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_UNIFIED_TRANSLATE.getCode())
                    .build();
        }
    }


    @ApiOperation(
            value = "99.4 updateSQL2_3_4_p24",
            notes = "线上模式添加打开承诺弹框。"
    )
    @ApiOperationSort(value = 5)
    @PostMapping("/addOpenPromise")
    public ApiResponse<Object> addOpenPromise() {
        try {

            iOfficeTaskExamSysConfigService.updateSQL2_3_4_p24();

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# updateSQL2_3_4_p24线上模式添加打开承诺弹框参数异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("updateSQL2_3_4_p24线上模式添加打开承诺弹框参数失败。", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_INIT_OPENPROMISE.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "99.5 升级系统区域编码",
            notes = "兼有现有的任务考点考场编码,执行前，请先初始化机构模板"
    )
    @ApiOperationSort(value = 6)
    @PostMapping("/updateAreaCode")
    public ApiResponse<Object> updateAreaCode() {
        try {

            iOfficeTaskExamSysConfigService.updateAreaCode();

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 升级系统区域编码异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("升级系统区域编码失败。", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_UPDATE_AREA_CODE.getCode())
                    .build();
        }
    }

}
